<template>
  <BasicModal v-bind="$attrs" destroyOnClose title="预约操作" width="50%" @register="registerModal" @ok="handleSubmit">
    <BasicForm @register="registerForm"></BasicForm>
    <Description class="mt-4" title="基本信息" :column="{ xxl: 4, xl: 3, lg: 3, md: 3, sm: 2, xs: 1 }" :data="delayriskInfo.data" :schema="delayriskInfoSchema" />
    <BasicTable @register="registerOrderDetailTable"></BasicTable>
  </BasicModal>
</template>
<script lang="ts" setup>
import { reactive } from 'vue';
import { BasicModal, useModalInner } from '@apps/antd-admin/src/components/modal';
import { DescItem, Description } from '@apps/antd-admin/src/components/description';
import { BasicColumn, BasicTable, useTable } from '@apps/antd-admin/src/components/table';
import { BasicForm, useForm } from '@apps/antd-admin/src/components/Form/index';
import { delayriskCheckSub, getDelayrisk } from '../data/api';
import { checkFormSchema } from '../data/data';

const emit = defineEmits(['success', 'register']);
let delayriskInfo: Recordable = reactive({ data: {} });
const delayriskInfoSchema: DescItem[] = [
  {
    field: 'delayriskNo',
    label: '延误险编号',
    span: 3,
  },
  {
    field: 'insurantNumber',
    label: '被保险人数量',
    span: 3,
  },
  {
    field: 'minorNumber',
    label: '未成年人数量',
    span: 3,
  },
  {
    field: 'startTime',
    label: '开始时间',
    span: 3,
  },
  {
    field: 'endTime',
    label: '结束时间',
    span: 3,
  },
  {
    field: 'phone',
    label: '联系电话',
    span: 3,
  },
  {
    field: 'appointmentState',
    label: '当前预约状态',
    span: 3,
    render: (curVal, data) => {
      let appointmentStateArray = [
        { label: '待购买', value: 0 },
        { label: '购买成功', value: 1 },
        { label: '购买失败', value: 2 },
      ];
      const appointmentState: any = appointmentStateArray.find(item => item.value == curVal);
      if (appointmentState) {
        return appointmentState.label;
      }
      return '';
    },
  },
];

const delayriskTableSchema: BasicColumn[] = [
  {
    title: '被保障人员',
    width: 150,
    dataIndex: 'userName',
  },
  {
    title: '证件类型',
    width: 150,
    dataIndex: 'identityType',
    customRender: ({ record }: Recordable) => {
      const identityTypeArray = [
        { label: '身份证', value: 1 },
        { label: '护照', value: 2 },
        { label: '回乡证(港澳台居民来往内地通行证)', value: 3 },
        { label: '台胞证', value: 4 },
        { label: '其他', value: 5 },
        { label: '军人证', value: 6 },
        { label: '外国居留身份证', value: 7 },
        { label: '港澳台居民居住证', value: 8 },
      ];
      const identityType: any = identityTypeArray.find(item => item.value == record.identityType);
      if (identityType) {
        return identityType.label;
      }
      return '';
    },
  },
  {
    title: '证件号',
    width: 150,
    dataIndex: 'identityNo',
  },
  {
    title: '出生日期',
    width: 150,
    dataIndex: 'birthDate',
  },
  {
    title: '性别',
    width: 80,
    dataIndex: 'sex',
    customRender: ({ record }: Recordable) => {
      const sexArray = [
        { label: '女', value: 1 },
        { label: '男', value: 2 },
      ];
      const sex: any = sexArray.find(item => item.value == record.sex);
      if (sex) {
        return sex.label;
      }
      return '';
    },
  },
];
const [registerOrderDetailTable, { setTableData }] = useTable({
  title: '延误保障人员',
  columns: delayriskTableSchema,
  pagination: false,
  showIndexColumn: false,
  scroll: { y: 300 },
  showSummary: false,
});

const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
  labelWidth: 150,
  labelCol: { span: 4 },
  schemas: checkFormSchema,
  showActionButtonGroup: false,
});

//详情赋值
const [registerModal, { setModalProps, closeModal }] = useModalInner(async data => {
  let record = data.record;
  let delayriskId = record.delayriskId;
  getDelayrisk({
    delayriskId: delayriskId,
    handlerName: 'busDelayriskDetailHandler',
  }).then(result => {
    console.info(result);
    delayriskInfo.data = result;
    setTableData(result.insurantList);
    resetFields();
    let delayriskInfoData = {
      delayriskId: delayriskId,
    };
    setFieldsValue({
      ...delayriskInfoData,
    });
  });
});

async function handleSubmit() {
  try {
    const values = await validate();
    setModalProps({ confirmLoading: true });
    await delayriskCheckSub(values);
    closeModal();
    emit('success', false);
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>
